package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    //查询全部吐槽列表
    @GetMapping()
    public Result findAllSpit(){
       List<Spit> spits= spitService.findAllSpit();
       return new Result(true,StatusCode.OK,"全部吐槽列表",spits);
    }

    //根据ID查询吐槽
    @GetMapping("/{spitId}")
    public Result findBySpitId(@PathVariable String spitId){
       Spit spit= spitService.findBySpitId(spitId);
       return new Result(true,StatusCode.OK,"根据ID查询吐槽",spit);
    }
    //增加吐槽
    @PostMapping()
    public Result addSpit(@RequestBody Spit spit){
        spitService.addSpit(spit);
        return new Result(true,StatusCode.OK,"新增吐槽");
    }
    //修改吐槽
    @PutMapping("{spitId}")
    public Result updateSpitId(@PathVariable String spitId,@RequestBody Spit spit){
        spitService.updateSpitId(spitId);
        return new Result(true,StatusCode.OK,"修改吐槽");
    }

    //根据ID删除吐槽
    @DeleteMapping("{spitId}")
    public Result deleteSpit(@PathVariable String spitId){
        spitService.deleteSpit(spitId);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    //根据上级ID查询吐槽列表
    @GetMapping("comment/{parentid}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
            PageResult<Spit> spitList=spitService.findByParentid(parentid,page,size);
           return new Result(true,StatusCode.OK,"上级ID查询吐槽列表",spitList);
    }

    //吐槽点赞
    @PutMapping("thumbup/{spitId}")
    public Result addThumbup(@PathVariable String spitId){
        Boolean aBoolean = spitService.addThumbup(spitId);
        System.out.println("aBoolean = " + aBoolean);;
        if(aBoolean){
            return new Result(true,StatusCode.OK,"吐槽点赞");
        }
        return new Result(false,StatusCode.ERROR,"吐槽点赞失败！");
    }

}
